//
//  UIScrollView+DZNEmpty.m
//  VICategory
//
//  Created by lan's Mac on 2022/7/22.
//

#import "UIScrollView+DZNEmpty.h"
#import <objc/runtime.h>
#import "UIScrollView+EmptyDataSet.h"//空页面
#import "UIImage+Category.h"

NSString * const  kVIEmptyDataTitle = @"kVIEmptyDataTitle";
NSString * const  kVIEmptyImageScale = @"kVIEmptyImageScale";
NSString * const  kVIEmptyDataImage = @"kVIEmptyDataImage";
NSString * const  kVIVerticalOffset = @"kVIVerticalOffset";
NSString * const  kVISpaceHeight = @"kVISpaceHeight";
NSString * const  kVIEmptyDataTitleColor = @"kVIEmptyDataTitleColor";
NSString * const  kVIEmptyDataTitleFont = @"kVIEmptyDataTitleFont";
NSString * const  kVIEmptyDataButtonTitle = @"kVIEmptyDataButtonTitle";
NSString * const  kVIEmptyDataButtonTitleColor = @"kVIEmptyDataButtonTitleColor";
NSString * const  kVIEmptyDataButtonFont = @"kVIEmptyDataButtonFont";
NSString * const  kVIEmptyDataTapBlock = @"kVIEmptyDataTapBlock";
NSString * const  kVIEmptyDataButtonClickBlock = @"kVIEmptyDataButtonClickBlock";

@interface UIScrollView (DZNEmpty) <DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@property (nonatomic, strong) NSMutableDictionary *configDict;

@end

static const char configDictionaryKey;
@implementation UIScrollView (DZNEmpty)

- (void)setConfigDict:(NSMutableDictionary *)configDict {
    objc_setAssociatedObject(self, &configDictionaryKey, configDict, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSMutableDictionary *)configDict {
    return objc_getAssociatedObject(self, &configDictionaryKey);
}

- (void)setEmptyData {
    [self setEmptyData:[self defaultData]];
}

- (NSMutableDictionary *)defaultData {
    NSDictionary *dict = @{kVIEmptyDataTitle : @"暂无数据",
                           kVIEmptyImageScale : @(1),
                           kVIEmptyDataImage:[UIImage imageNamed:@"common_empty_data"],
                           kVISpaceHeight:@(11),
                           kVIVerticalOffset:@(-130),
                           kVIEmptyDataTitleColor:UIColor.lightGrayColor,
                           kVIEmptyDataButtonTitle:@"",
                           kVIEmptyDataButtonTitleColor:UIColor.redColor,
                           kVIEmptyDataButtonFont:[UIFont systemFontOfSize:16.0],
                           kVIEmptyDataTitleFont:[UIFont systemFontOfSize:13.0]
    };
    
    return [NSMutableDictionary dictionaryWithDictionary:dict];
}

- (void)setEmptyData:(NSDictionary *)configDict {
    NSMutableDictionary *mulDict = [self defaultData];
    [mulDict addEntriesFromDictionary:configDict];
    self.configDict = mulDict;
    self.emptyDataSetSource = self;
    self.emptyDataSetDelegate = self;
}

- (void)clearEmpty {
    self.emptyDataSetDelegate = nil;
    self.emptyDataSetSource = nil;
    [self reloadEmptyDataSet];
}

#pragma mark - DZNEmptyDataSetSource
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    UIImage *image = self.configDict[kVIEmptyDataImage];
    image = [image imageCompressScale:[self.configDict[kVIEmptyImageScale] floatValue]];
    return image;
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *text = [NSString stringWithFormat:@"%@",self.configDict[kVIEmptyDataTitle]];;
    NSDictionary *attibutes = @{NSFontAttributeName:self.configDict[kVIEmptyDataTitleFont],
                                NSForegroundColorAttributeName:self.configDict[kVIEmptyDataTitleColor]
    };
    return [[NSAttributedString alloc] initWithString:text attributes:attibutes];
}

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
    NSString *text = [NSString stringWithFormat:@"%@",self.configDict[kVIEmptyDataButtonTitle]];
    NSDictionary *attributes = @{NSFontAttributeName: self.configDict[kVIEmptyDataButtonFont],
                                 NSForegroundColorAttributeName:self.configDict[kVIEmptyDataButtonTitleColor]
    };
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return [self.configDict[kVIVerticalOffset] floatValue];
}

- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView {//图片和字体的间距
    return [self.configDict[kVISpaceHeight] floatValue];
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    void (^tapAction) (void) = self.configDict[kVIEmptyDataTapBlock];
    if (tapAction) tapAction();
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button {
    void (^tapButtonAction) (void) = self.configDict[kVIEmptyDataButtonClickBlock];
    if (tapButtonAction) tapButtonAction();
}
@end
